package com.cskaoyan.gateway.controller.shopping;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cskaoyan.gateway.form.shopping.OrderDetail;
import com.cskaoyan.gateway.form.shopping.PageInfo;
import com.cskaoyan.gateway.form.shopping.PageResponse;
import com.mall.commons.result.ResponseData;
import com.mall.commons.result.ResponseUtil;
import com.mall.order.OrderCoreService;
import com.mall.order.OrderQueryService;
import com.mall.order.constant.OrderRetCode;
import com.mall.order.dto.*;
import com.mall.user.intercepter.TokenIntercepter;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("/shopping")
@Slf4j
public class OrderController {

    @Reference
    OrderCoreService orderCoreService;

    @Reference
    OrderQueryService orderQueryService;


    /**
     * 创建订单
     */
    @PostMapping("/order")
    @ApiOperation("创建订单")
    public ResponseData order(@RequestBody CreateOrderRequest createOrderRequest, HttpServletRequest httpServletRequest){
        JSONObject jsonObject = JSON.parseObject((String) httpServletRequest.getAttribute(TokenIntercepter.USER_INFO_KEY));
        Long uid = Long.parseLong(jsonObject.get("uid").toString());
        createOrderRequest.setUserId(uid);

        createOrderRequest.setUniqueKey(UUID.randomUUID().toString());
        CreateOrderResponse response = orderCoreService.createOrder(createOrderRequest);
        if (response.getCode().equals(OrderRetCode.SUCCESS.getCode())){
            return new ResponseUtil<>().setData(response.getOrderId());
        }

        return new ResponseUtil<>().setErrorMsg(response.getMsg());

    }

    /**
     * 获取当前用户所有订单
     */
    @GetMapping("/order")
    @ApiOperation("获取当前用户所有订单")
    public ResponseData order(PageInfo pageInfo, HttpServletRequest httpServletRequest){
        OrderListRequest orderListRequest = new OrderListRequest();
        orderListRequest.setPage(pageInfo.getPage());
        orderListRequest.setSize(pageInfo.getSize());
        orderListRequest.setSort(pageInfo.getSort());

        JSONObject jsonObject = JSON.parseObject((String) httpServletRequest.getAttribute(TokenIntercepter.USER_INFO_KEY));
        Long uid = Long.parseLong(jsonObject.get("uid").toString());
        orderListRequest.setUserId(uid);

        OrderListResponse orderListResponse = orderQueryService.orderList(orderListRequest);

        if (orderListResponse.getCode().equals(OrderRetCode.SUCCESS.getCode())){
            PageResponse pageResponse = new PageResponse();
            pageResponse.setData(orderListResponse.getDetailInfoList());
            pageResponse.setTotal(orderListResponse.getTotal());
            return new ResponseUtil<>().setData(pageResponse);
        }

        return new ResponseUtil<>().setErrorMsg(orderListResponse.getMsg());

    }

    /**
     * 查询订单详情
     */
    @GetMapping("/order/{id}")
    @ApiOperation("查询订单详情")
    public ResponseData orderDetail(@PathVariable("id") String orderId){
        OrderDetailRequest orderDetailRequest = new OrderDetailRequest();
        orderDetailRequest.setOrderId(orderId);

        OrderDetailResponse orderDetailResponse = orderQueryService.orderDetail(orderDetailRequest);

        if (orderDetailResponse.getCode().equals(OrderRetCode.SUCCESS.getCode())){
            OrderDetail orderDetail = new OrderDetail();
            orderDetail.setOrderTotal(orderDetailResponse.getPayment());
            orderDetail.setUserId(orderDetailResponse.getUserId());
            orderDetail.setUserName(orderDetailResponse.getBuyerNick());
            orderDetail.setGoodsList(orderDetailResponse.getOrderItemDto());
            orderDetail.setTel(orderDetailResponse.getOrderShippingDto().getReceiverPhone());
            orderDetail.setStreetName(orderDetailResponse.getOrderShippingDto().getReceiverAddress());
            orderDetail.setOrderStatus(orderDetailResponse.getStatus());
            return new ResponseUtil<>().setData(orderDetail);
        }

        return new ResponseUtil<>().setErrorMsg(orderDetailResponse.getMsg());
    }

    /**
     * 取消订单
     */
    @PostMapping("/cancelOrder")
    @ApiOperation("取消订单")
    public ResponseData cancelOrder(@RequestBody Map<String,String> map){
        String uid = map.get("orderId");
        CancelOrderRequest cancelOrderRequest = new CancelOrderRequest();
        cancelOrderRequest.setOrderId(uid);
        CancelOrderResponse cancelOrderResponse = orderCoreService.cancelOrder(cancelOrderRequest);
        return new ResponseUtil<>().setData(cancelOrderResponse);
    }

    /**
     * 删除订单
     */
    @DeleteMapping("/order/{id}")
    @ApiOperation("删除订单")
    public ResponseData deleteOrder(@PathVariable String id){
        DeleteOrderRequest deleteOrderRequest = new DeleteOrderRequest();
        deleteOrderRequest.setOrderId(id);
        DeleteOrderResponse deleteOrderResponse = orderCoreService.deleteOrder(deleteOrderRequest);
        return new ResponseUtil<>().setData(deleteOrderResponse);
    }





}
